package com.example.h.weixinui3;

import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.ImageButton;
import android.widget.RadioGroup;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    //片段管理器
    private FragmentManager fm;
    //片段事务对象
    private FragmentTransaction trans;

    //获取单选按钮组
    private RadioGroup rg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rg=(RadioGroup)findViewById(R.id.radgroup);

        //1：获取片段管理器对象
        fm=getFragmentManager();
        //2:获取片段事务对象
        trans=fm.beginTransaction();
        trans.add(R.id.mainFrame,new OneFragment());
        trans.commit();

        //添加选择改变事件
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rad_1:
                        trans=fm.beginTransaction();
                        trans.replace(R.id.mainFrame,new OneFragment());
                        trans.addToBackStack("back");
                        trans.commit();
                        break;
                    case R.id.rad_2:
                        trans=fm.beginTransaction();
                        trans.replace(R.id.mainFrame,new TwoFragment());
                        trans.addToBackStack("back");
                        trans.commit();
                        break;
                    case R.id.rad_3:
                        trans=fm.beginTransaction();
                        trans.replace(R.id.mainFrame,new ThreeFragment());
                        trans.addToBackStack("back");
                        trans.commit();
                        break;
                    case R.id.rad_4:
                        trans=fm.beginTransaction();
                        trans.replace(R.id.mainFrame,new FourFragment());
                        trans.addToBackStack("back");
                        trans.commit();
                        break;
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        setIconEnable(menu,true);//设置显示图像
        getMenuInflater().inflate(R.menu.menu_option,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home)
            this.finish();
        return super.onOptionsItemSelected(item);
    }

    private void setIconEnable(Menu menu, boolean enable)
    {
        
        try{
            Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
            Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
            m.setAccessible(true);
            //下面传入参数
            m.invoke(menu, enable);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private ImageButton imageButton4,imageButton6,imageButton7;

    public void adds(final View view){
        AlertDialog.Builder builder=new AlertDialog.Builder(this,R.style.Dialog_Fullscreen);//fullscreen全屏
        //builder.setTitle("");
        View dialigView=getLayoutInflater().from(this).inflate(R.layout.layout_adds,null);
        imageButton6=(ImageButton) dialigView.findViewById(R.id.imageButton6);
        imageButton6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(MainActivity.this,SendActivity.class);
                startActivity(intent);
            }
        });
        builder.setView(dialigView);
        builder.create().show();
       /*
        this.imageButton4=(ImageButton) super.findViewById(R.id.imageButton4);
        this.imageButton4.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    Intent intent=new Intent(getParent(),SendActivity.class);
                    startActivity(intent);
                }
            }
        });
        this.imageButton6=(ImageButton) super.findViewById(R.id.imageButton6);
        this.imageButton7=(ImageButton) super.findViewById(R.id.imageButton7);
        */
       /*AlertDialog menuDialog = new AlertDialog.Builder(MainActivity.this, R.style.Dialog_Fullscreen).create();
        menuDialog.show();
        Window window = menuDialog.getWindow();
        window.setContentView(R.layout.layout_adds);
        menuDialog.setCanceledOnTouchOutside(true);
        this.imageButton4=(ImageButton) window.findViewById(R.id.imageButton4);
        imageButton4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getIntent(),SendActivity.class);
            }
        }*/
    }


}
